﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Query2Object;

namespace SqlServer2005
{
    class SqlContext:IDbContext
    {
        private string connectionstring;

        #region IDbContext 成员

        public ISession GetSession()
        {
            try
            {
                ISession sa = new SqlSession();
                sa.Context = this;
                sa.Init(connectionstring);
                SessionPool.Current.Push(Thread.CurrentThread.ManagedThreadId, sa);
                return sa;
            }
            catch (SessionException ex)
            {
                throw new dbContextException("init session error", ex);
            }
            catch (Exception ex)
            {
                throw new dbContextException("unkown error when getsession", ex);
            }
        }

        public IStruts TableSchema()
        {
            try
            {
                return new SqlStruts(connectionstring);
            }
            catch (CodeGenerateException ex)
            {
                throw new dbContextException("get codegenerator error", ex);
            }
            catch (Exception ex)
            {
                throw new dbContextException("unkown error when get table schema!", ex);
            }
        }

        public String ConnectionString
        {
            get
            {
                //throw new NotImplementedException();
                return connectionstring;
            }
            set
            {
                //throw new NotImplementedException();
                connectionstring = value;
            }
        }

        #endregion
    }
}
